﻿using Blazored.LocalStorage;
using Bookify.Web.DTO.Bookings;
using Bookify.Web.Services.Base;
using Bookify.Web.Services.Share;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using DateOnly = System.DateOnly;

namespace Bookify.Web.Services.Bookings
{
    public class BookingCaller : BaseHttpService
    {
        private readonly AuthenticationStateProvider _authenticationStateProvider;

        public BookingCaller(IClient client, 
            ILocalStorageService localStorageService, 
            IOptions<ServiceOptions> options, 
            AuthenticationStateProvider authenticationStateProvider) : base(client, localStorageService, options)
        {
            this._authenticationStateProvider = authenticationStateProvider;
        }

        public async Task<Response<Guid>> ReserveBookingAsync(Guid ApartmentId,DateOnly StartDate, DateOnly EndDate)
        {

            try
            {
                var response = new Response<Guid>();

                var authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
                Guid UserId;
                Guid.TryParse(authState.User?.Identity?.Name, out UserId);

                ReserveBookingRequest bookingRequest = new ReserveBookingRequest() { ApartmentId = ApartmentId, UserId = UserId, StartDate =StartDate, EndDate = EndDate };
                
                await _client.BookingsPOSTAsync(bookingRequest);
                
                return response;
            }
            catch (ApiException ex)
            {
                return new Response<Guid>() { Message = ex.Message, Success = false }; 
            }
            //var response = await client.PostAsJsonAsync("/api/Bookings", request);

            //if (response.IsSuccessStatusCode)
            //{
            //    return true;
            //}
            //else
            //{
            //    return false;
            //}
        }
    }
}
